﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using REVISIONMAX30.DataObjects;
using FrameWork.Lib;
using FrameWork.Lib.DataObjects;
using FrameWork.WinUI.Common;
using FrameWork.WinUI.Controls;

namespace REVISIONMAX30.Controls
{
    public partial class MySourceDirectoryList : UserControl
    {
        public MySourceDirectoryList()
        {
            InitializeComponent();
        }


        public UIManager UIManager { get; set; }
        public DataObjects.Group Group { get; set; }

        public void Init(DataObjects.Group pParentObject)
        {
            this.Group = pParentObject;
            this.UpdateViewer();
        }


        private void btnDelete_Click(object sender, EventArgs e)
        {
            var tSelectedGuidList = this.lvSources.SelectedItems.Cast<ListViewItem>().Select(t => t.Tag).Cast<Guid>().ToList();
            this.Group.SourceDirectoryList.RemoveAll(t => tSelectedGuidList.Contains(t.DataObjectGUID));

            this.UpdateViewer();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            this.Edit();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {

            var tfrmBrowseDirectory = new FrameWork.WinUI.frmBrowseDirectory();

            var tDir = tfrmBrowseDirectory.View("");

            if (tDir != null)
            {
                this.UIManager.ViewSourceDirectory(new SourceDirectory(tDir), this.Group);
                this.UpdateViewer();
            }

 
            
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            this.UpdateViewer();
        }


        private void Edit()
        {

            if (this.lvSources.SelectedItems.Count > 0)
            {
                var tSelectedGUID = (Guid)this.lvSources.SelectedItems[0].Tag;
                this.UIManager.ViewSourceDirectory(this.Group.SourceDirectoryList.Where(t => t.DataObjectGUID == tSelectedGUID).First(), this.Group);
                this.UpdateViewer();
            }
        }


        private void lvSources_DoubleClick(object sender, EventArgs e)
        {
            this.Edit();
        }

        public void UpdateViewer()
        {
            this.lvSources.Items.Clear();

            foreach (var tSourceDirectory in this.Group.SourceDirectoryList)
            {
                var tlvItem = this.lvSources.Items.Add(tSourceDirectory.DirectoryName);
                tlvItem.Tag = tSourceDirectory.DataObjectGUID;
                tlvItem.SubItems.Add(tSourceDirectory.ScanDirectoryRecusivly.BoolValueYesNo());
                tlvItem.SubItems.Add(tSourceDirectory.Created.StringValueOrEmpty("MM/dd/yyyy HH:mm"));
                tlvItem.SubItems.Add(tSourceDirectory.Updated.StringValueOrEmpty("MM/dd/yyyy HH:mm"));

                if (!tSourceDirectory.DirectoryExists())
                {
                    tlvItem.BackColor = Color.LightSalmon;
                }

            }

            if (this.lvSources.Items.Count > 0)
            {
                this.lvSources.Columns[0].AutoResize();
                this.lvSources.Columns[1].AutoResize();
                this.lvSources.Columns[2].AutoResize();
                this.lvSources.Columns[3].AutoResize();
         
            }
        }

    }
}
